alerts.js
/common/alerts.js is a common file to both backend repos to POST new alerts and PATCH existing ones. It also contains a cache JSON object used to delay the posting of new alerts for a declared number of run cycles.
Features
- POST new alerts
- PATCH existing alerts
- Determines if an alert can be closed
- Delay new alerts
Cache delay
Delay the POSTING of certain alerts by way of the delayJSON object. The alerts to delay are held in a configuration file stored in the database. Use teh Centurion UI to updated and refine those alerts to delay.
{
"notes": "Changes require a restart of centurionV2-workers.",
"templates": {
"createPricesNoDataAlertObj": 3,
"createWalletsNoDataAlertObj": 3
}
}
The configuration file is used to populate the internal JSON object that becomes the delayCache object.
let delayJSON = {
templates: { createPricesNoDataAlertObj: 3, createWalletsNoDataAlertObj: 3 },
aliases: {},
};
How it works:
-
When a new alert is inbound, and the alert's template is in the
delayJSONobject, the alert will be delayed. -
If the alert's template exists but the alert's
aliasdoes not, then thealiasis added to the keyaliasesalong with a counter value of 1. The cache waits for the worker's next run cycle.{templates: { "createPricesNoDataAlertObj": 3,"createWalletsNoDataAlertObj": 3 },aliases: {"walletsWorker-348348877", 1},} -
On the worker's next run cycle, if the
aliascounter does not equal the template counter, then thealiascounter is incremented and the alert is not POSTED. The cache waits for the worker's next run cycle. -
On the worker's next run cycle, if the
aliascounter equals the template counter, then the alert is posted.
Clear the alias
It is up to the worker to clear its own aliases when a condition no longer exists.
const { createWalletsNoDataAlertObj, getBaseAlias } = require("./alertTemplates");
...
if (
!chain.testnet && alert.data.length === Object.keys(chain.providers.rpc).length
){
sendAlert(await createWalletsNoDataAlertObj(alert));
}
else {
removeAlias(getBaseAlias(chain.network_id));
}